<template>
	<tm-fullView bg-color="#ffffff">
		<tm-menubars :title="$t('登录')" iconColor="white" color="#ffffff" :showback="false">
			<view slot="right" style="padding-right: 20rpx;">
				<tm-icons @click="show=true" size="40" name="myicon-zhongyingqiehuan"></tm-icons>
			</view>
		</tm-menubars>
		<view class="logo">
			<image src="/static/logo.png"></image>
		</view>
		<view class="login-form">
			<tm-input name="title" 
			prefixp-icon="myicon-youjianyouxiang" 
			prefixp-icon-color="grey-darken-1" 
			prefixp-icon-size="40"
			:placeholder="$t('请输入邮箱')" 
			:height="80" :round="20" 
			:border-bottom="false" 
			bg-color="grey-lighten-3" 
			v-model="reqData.email"/>
			<view style="height:40rpx;"></view>
			
			<tm-input name="title" 
			prefixp-icon="myicon-mima" 
			prefixp-icon-color="grey-darken-1" 
			prefixp-icon-size="40"
			input-type="password"
			:placeholder="$t('请输入密码')" 
			:height="80" 
			:round="20" 
			:border-bottom="false" 
			bg-color="grey-lighten-3" 
			v-model="reqData.password"/>
			<view class="login-submit">
				<tm-button theme="grey-darken-2" :loading="loading" @click="login()" :round="24" block>{{$t("登录")}}</tm-button>
				<view class="agreement">
					<tm-checkbox v-model="reqData.checked" :dense="true" :size="24"></tm-checkbox>
					<text class="text">{{$t('请阅读并同意')}}</text>
					<text class="link">{{$t('用户协议')}}</text>
					<text class="text">{{$t('和')}}</text>
					<text class="link">{{$t('隐私协议')}}</text>
				</view>
			</view>
		</view>
		<view class="third">
			<tm-grid color="blue" :list="list2" ></tm-grid>
		</view>
		<view class="flex-center mt-30">
			<text @click="navto('/pages/forget/forget')" class="text-size-s text-grey-darken-1">{{$t('找回密码')}}</text>
			<text class="text-size-s text-grey-darken-1">｜</text>
			<text @click="navto('/pages/register/register')" class="text-size-s text-grey-darken-1">{{$t('注册用户')}}</text>
		</view>
		<tm-actionSheetMenu :title="$t('请选择语言')"  @change="changeLang" v-model="show" :actions="langs"></tm-actionSheetMenu>
	</tm-fullView>
</template>

<script>
	import IndexApi from '@/api/index.js'
	export default {
		data() {
			return {
				show:false,
				loading:false,
				reqData:{
					email:'2518687673@qq.com',
					password:'123456',
					checked:true
				},
				list2:[
					{icon:'myicon-wechat',text:'wechat',color:'grey-darken-1'},
					{icon:'myicon-tumblr1',text:'twitter',iconSize:40,color:'grey-darken-1'},
					{icon:'myicon-facebookfacebook52',text:'facebook',color:'grey-darken-1'},
				]
			}
		},
		computed:{
			langs(){
				var langList=this.$tm.vx.state().app.config.langList
				let _langs=[]
				if(langList){
					langList.forEach(item=>{
						_langs.push(item.name)
					})
					return _langs
				}
				return _langs
			},
		},
		methods: {
			async changeLang(e){
				let self=this
				self.show=false
				uni.showLoading({
					title:self.$t('正在加载语言')
				})
				var lang=self.$tm.vx.state().app.config.langList[e.index]
				uni.setStorageSync("lang",lang.varName)
				self.$tm.vx.commit('translate/setDefaultLang',lang.varName)
				//初始化翻译
				var res=await self.$tm.vx.actions('translate/getTranslateList')
				uni.setStorageSync('translateList',res)
				setTimeout(()=>{
					uni.hideLoading()
					uni.showToast({
						title:self.$t('语言切换成功')
					})
					self.setTabbar()
					self.defaultLang=lang.name
				},1000)
			},
			login(){
				let self=this
				if(this.reqData.email==''){
					uni.showToast({
						title:self.$t('请输入你的邮箱'),
						icon:'none'
					})
					return
				}
				if(this.reqData.password==''){
					uni.showToast({
						title:self.$t('请输入密码'),
						icon:'none'
					})
					return
				}
				if(!this.reqData.checked){
					uni.showToast({
						title:self.$t('请同意用户协议和隐私协议'),
						icon:'none'
					})
					return
				}
				self.loading=true
				IndexApi.login(self.reqData).then(res=>{
					self.loading=false
					uni.showToast({
						title:res.msg,
						icon:'none'
					})
					if(res.code==200){
						uni.setStorageSync("token",res.data)
						self.$tm.vx.actions('user/getUserInfo')
						uni.switchTab({
							url:'/pages/index/index'
						})
					}
				}).catch(e=>{
					self.loading=false
					uni.showToast({
						title:self.$t('网络异常'),
						icon:'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
.logo{
	display: flex;
	flex-direction: row;
	justify-content: center;
	align-items: center;
	padding: 80rpx 20rpx;
	image{
		width: 160rpx;
		height: 160rpx;
	}
}
.login-form{
	padding: 20rpx 40rpx;
	.login-submit{
		margin-top: 80rpx;
		padding: 0 40rpx;
		.agreement{
			margin-top: 30rpx;
			display: flex;
			flex-direction: row;
			align-items: center;
			padding-left: 20rpx;
			.text{
				font-size: 24rpx;
			}
			.link{
				font-size: 24rpx;
				color: #2196F3;
				text-decoration: underline
			}
		}
	}
}
.third{
	text-align: center;
	margin-top: 130rpx;
}
</style>
